<template>
  <div class="base-container">
    <t-form ref="form" :data="queryParams" :label-width="80" @submit="()=>{this.pagination.current=1;this.queryParams.pageNum=1;this.getList()}" @reset="()=>{this.pagination.current=1;this.queryParams.pageNum=1;this.getList()}">
      <t-row style="border-bottom: 1px solid #E5E6EB;margin: 10px 0px">
        <t-col :span="11" class="query-form-inline" >
          <t-form-item label="所属年度" name="year">
            <t-date-picker v-model="queryParams.year" mode="year" clearable/>
          </t-form-item>
        </t-col>
        <t-col :span="1" class="operation-container">
          <t-button theme="primary" type="submit"> 查询</t-button>
          <t-button theme="default" variant="outline" type="reset">重置</t-button>
        </t-col>
      </t-row>
    </t-form>
    <div class="btn-group-inline">
      <t-button @click="dialog.header='新增年度奖励';dialog.objId=0;dialog.open_add_edit_year_award=true"  variant="outline" theme="primary">
        <t-icon name="add" slot="icon"/>
        新增年度奖励
      </t-button>
    </div>
    <t-table
      rowKey="id"
      :data="data"
      :columns="columns"
      hover
      :pagination="pagination"
      :loading="dataLoading"
    >
      <template #awardsAllCount="{row}">
        {{ row.awardsAllCount }}项
<!--        {{row.oneAwardsNum+row.twoAwardsNum+row.threeAwardsNum}}项-->
      </template>
      <template #awardsAllAmount="{row}">
        {{ row.awardsAllAmount }}万元
<!--        {{row.oneAwardsNum*row.oneAwardsAmount+row.twoAwardsNum*row.twoAwardsAmount+row.threeAwardsNum*row.threeAwardsAmount}}万元-->
      </template>
      <template  #oneAwardsNum="{ row }">
        {{row.oneAwardsNum}}项<br/>
        每项奖金{{row.oneAwardsAmount}}万元<br/>
        已评奖{{row.oneRecommended}}项<br/>
      </template>
      <template  #twoAwardsNum="{ row }">
        {{row.twoAwardsNum}}项<br/>
        每项奖金{{row.twoAwardsAmount}}万元<br/>
        已评奖{{row.twoRecommended}}项<br/>
      </template>
      <template  #threeAwardsNum="{ row }">
        {{row.threeAwardsNum}}项<br/>
        每项奖金{{row.threeAwardsAmount}}万元<br/>
        已评奖{{row.threeRecommended}}项<br/>
      </template>
      <template  #isPublicity="{ row }">
        <t-switch @change="handleChange(row)" v-model="row.isPublicity" :customValue="['1','0']"></t-switch>
      </template>
      <template  #op="{ row }">
        <t-button  @click="dialog.header='修改年度奖励';dialog.objId=row.id;dialog.open_add_edit_year_award = true;" size="small" variant="text"
                  class="t-button-link">
          <t-icon name="edit-1" slot="icon"/>
          修改
        </t-button>
        <t-button v-if="row.isPublicity === '0'" @click="toReview(row)" size="small" variant="text"
                  class="t-button-link">
          <t-icon name="edit-1" slot="icon"/>
          评奖
        </t-button>
        <t-button v-else @click="dialog.header='评奖';dialog.editOpen=false;dialog.objId=row.id;dialog.year=row.year;dialog.open_innovation_award = true;" size="small" variant="text"
                  class="t-button-link">
          <t-icon name="info-circle" slot="icon"/>
          评奖详情
        </t-button>
         <t-button theme="danger" @click="delObj(row)" size="small" variant="text" class="t-button-link">
          <t-icon name="delete" slot="icon"/>
          删除
        </t-button>
      </template>
    </t-table>
    <innovation-award :header="dialog.header" :editOpen="dialog.editOpen" :objId="dialog.objId" :year="dialog.year" :visible.sync="dialog.open_innovation_award" v-if="dialog.open_innovation_award"></innovation-award>
    <add-edit-year-innovation-award @reload="getList" :edit-open="true" :header="dialog.header" :objId="dialog.objId"  :visible.sync="dialog.open_add_edit_year_award" v-if="dialog.open_add_edit_year_award"></add-edit-year-innovation-award>

  </div>
</template>

<script>
import InnovationAward from "./components/innovation-award.vue";
import AddEditYearInnovationAward from "./components/add-edit-year-innovation-award.vue";

export default {
  name: "apply-award",
  components:{
    InnovationAward,
    AddEditYearInnovationAward
  },
  data(){
    return {
      dialog: {
        open_add_edit_year_award: false,
        open_innovation_award: false,
        year:null,
        editOpen: true,
      },
      data:[

      ],
      columns:[
        {
          colKey: "year",
          title: "年度",
          align: "center",
          ellipsis: true,
          width: 120
        },
        {
          colKey: "awardsAllCount",
          title: "总奖项",
          align: "center",
          // width: 100
        },
        {
          colKey: "awardsAllAmount",
          title: "总奖金",
          align: "center",
          // width: 100
        },
        // {
        //   colKey: "oneAwardsNum",
        //   title: "一等奖",
        //   align: "center",
        //   ellipsis: true,
        // },
        // {
        //   colKey: "twoAwardsNum",
        //   title: "二等奖",
        //   align: "center",
        //   ellipsis: true,
        // },
        // {
        //   colKey: "threeAwardsNum",
        //   title: "三等级",
        //   align: "center",
        //   ellipsis: true,
        // },
        {
          colKey: "isPublicity",
          title: "是否公示",
          align: "center",
          ellipsis: true,
        },
        {
          colKey: "createTime",
          title: "创建时间",
          align: "center",
          ellipsis: true
        },
        {
          colKey: "op",
          title: "操作",
          align: "center",
          width: 110
        },
      ],
      dataLoading: false,
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        year:''
      },
      pagination: {
        current: 1,
        pageSize: 10,
        total: 0,
        showPageSize: true,
        pageSizeOptions: [10, 20, 30, 50],
        showFirstAndLastPageBtn: false,
        onChange: (pageInfo) => {
          Object.assign(this.pagination,pageInfo)
          this.queryParams.pageNum = pageInfo.current || 1;
          this.queryParams.pageSize = pageInfo.pageSize;
          this.getList()
        },
      },
    }
  },
  created() {
    this.getList();
  },
  methods: {
    getList() {
      this.dataLoading = true;
      this.$api.wcx.apply.annualAwardsList(this.queryParams).then(
        (res) => {
          this.data = res.rows;
          this.pagination.total = res.total;
          this.dataLoading = false;
        }).catch((e) => {
        this.dataLoading = false;
        this.$message.error(e.toString());
      });
    },
    handleChange(row) {
      const text = row.isPublicity === "0" ? "取消公示" : "公示";
      const confirmDia = this.$dialog.confirm({
        header: "系统提示",
        body: `确认要${text}"${row.year}年度奖励"吗？`,
        theme: "warning",
        onConfirm: ({ e }) => {
          confirmDia.update({
            confirmBtn: {
              loading: true,
            },
          })
          this.$api.wcx.apply.innovationAwardsPublicity({
            id: row.id,
            isPublicity:row.isPublicity,
          }).then(
            (res) => {
              this.$message.success(`${text}成功`);
              // 请求成功后，销毁弹框
              confirmDia.destroy();
            }
          ).catch((e) => {
            row.isPublicity = row.isPublicity === "0" ? "1" : "0";
            confirmDia.update({
              confirmBtn: {
                loading: false,
              },
            })
            this.$message.error(e.toString());
          });

        },
        onClose: ({ e, trigger }) => {
          row.isPublicity = row.isPublicity === "0" ? "1" : "0";
          confirmDia.hide();
        },
      });
    },
    toReview(row){
      this.$router.push({
        path: `/wcx/other/award/year-review/${row.id}`,
        query: {
          title: `${row.year}年度-微创新成果评奖`
        }
      })
      // dialog.header='评奖';dialog.editOpen=true;dialog.objId=row.id;dialog.year=row.year;dialog.open_innovation_award = true;
    },
    delObj(row){
      const confirmDia = this.$dialog.confirm({
        header: "系统提示",
        body: `是否确认删除"${row.year}"年度奖励项？`,
        theme: "warning",
        onConfirm: () => {
          console.log(66)
          this.$api.wcx.apply.annualAwardsDel(row.id).then(
            (res) => {
              this.getList();
              this.$message.success("删除成功");
              // 请求成功后，销毁弹框
              confirmDia.destroy();
            }
          ).catch((e) => {
            this.$message.error(e.toString());
          });
        }
      });
    }
  }
}
</script>

<style scoped>

</style>
